package com.wyk.service.impl;

import com.wyk.domain.Order;
import com.wyk.mapper.OrderMapper;
import com.wyk.service.AccountService;
import com.wyk.service.OrderService;
import com.wyk.service.StrorageService;
import io.seata.spring.annotation.GlobalTransactional;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author WangYuKai
 * @Date 2022/8/4
 * @apiNote
 */
@Service
@Slf4j
public class OrderServiceImpl implements OrderService {
    //注入mapper
    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private AccountService accountService;
    @Autowired
    private StrorageService strorageService;
    @Override
    @GlobalTransactional(name = "fsp-create-order",rollbackFor = Exception.class)//全局事务
    public int addOrder(Order order) {
        //创建订单
        int i = orderMapper.addOrder(order);
        if (i != 0){
            //调用库存
            strorageService.decrease(order.getProuctId(),order.getCount());
            //账户余额做扣减
            accountService.decrease(order.getUserId(),order.getMoney());
            //修改订单状态
            orderMapper.updateOrderStatus(order.getUserId(),0);
            return 1;
        }
        return 0;
    }

    @Override
    public int updateOrderStatus(Long userId, Integer status) {
        return 0;
    }
}
